package base

import (
	"context"
	"git.mycaigou.com/gfyx/common/tenant_db"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/errcode"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/repository/remote/gfyx_user_service_remote"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/base"
	tenantPb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service/tenant"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/profit"
	userService "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/base/user"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type Login struct {
	loginService        userService.LoginServiceIface
	profitService       profit.ProfitServiceIface
	gfyxUserServiceRepo gfyx_user_service_remote.GfyxUserServiceRepositoryIface
}

// NewLogin 构造函数
func NewLogin(
	loginService userService.LoginServiceIface,
	profitService profit.ProfitServiceIface,
	gfyxUserServiceRepo gfyx_user_service_remote.GfyxUserServiceRepositoryIface,
) pb.UserLoginServiceServer {
	return &Login{
		loginService:        loginService,
		profitService:       profitService,
		gfyxUserServiceRepo: gfyxUserServiceRepo,
	}
}

// LoginByPassword 账号密码登录
func (l *Login) LoginByPassword(ctx context.Context, request *pb.LoginByPasswordRequest) (*pb.LoginByPasswordResponse, error) {
	resp, err := l.loginService.LoginByPassword(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc LoginByPassword request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc LoginByPassword request:%+v, response:%#v", request, resp)
	return resp, nil
}

// LoginByPhone 手机号验证码登录
func (l *Login) LoginByPhone(ctx context.Context, request *pb.LoginByPhoneRequest) (*pb.LoginByPhoneResponse, error) {
	resp, err := l.loginService.LoginByPhone(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc LoginByPhone request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc LoginByPhone request:%+v, response:%#v", request, resp)
	return resp, nil
}

// GetLoginUser 获取登录用户
func (l *Login) GetLoginUser(ctx context.Context, request *pb.LoginUserRequest) (*pb.LoginUserInfo, error) {
	info, err := l.loginService.GetLoginUserInfo(ctx, request)
	// 获取用户信息
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetLoginUser request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	// 获取租户信息 从上下文获取
	info.TenantCode, err = tenant_db.GetTenantCode(ctx)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetLoginUser request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	tenantInfo, err := l.gfyxUserServiceRepo.GetTenantByCode(ctx, &tenantPb.GetTenantByCodeRequest{Code: info.TenantCode})
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetTenantInfoByTenantCode failed, err:%s", err.Error())
		return nil, ykerrcode.TogRPCError(errcode.COMMON_GET_TENANT_ERROR, "获取用户名称失败")
	}
	info.TenantName = tenantInfo.Name
	// 获取租户权益
	info.Profits, err = l.profitService.GetTenantProfit(ctx)
	if err != nil {
		return nil, ykerrcode.TogRPCError(errcode.PROFIT_GET_LIST_ERROR, "获取权益失败")
	}

	return info, nil
}

// Logout 退出登录
func (l *Login) Logout(ctx context.Context, request *pb.LogoutRequest) (*pb.LogoutResponse, error) {
	resp, err := l.loginService.LogoutUser(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc Logout request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// RefreshToken 刷新 token
func (l *Login) RefreshToken(ctx context.Context, request *pb.RefreshTokenRequest) (*pb.RefreshTokenResponse, error) {
	resp, err := l.loginService.RefreshLoginToken(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc RefreshToken request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// GetResetPasswordToken 获取重置密码 token
func (l *Login) GetResetPasswordToken(ctx context.Context, request *pb.GetResetPasswordTokenRequest) (*pb.GetResetPasswordTokenResponse, error) {
	resp, err := l.loginService.GetResetPasswordToken(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetResetPasswordToken request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// ResetPasswordByPhone 重设用户密码
func (l *Login) ResetPasswordByPhone(ctx context.Context, request *pb.ResetPasswordByPhoneRequest) (*pb.ResetPasswordByPhoneResponse, error) {
	resp, err := l.loginService.ResetPasswordByPhone(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ResetPasswordByPhone request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc ResetPasswordByPhone request:%+v, response:%#v", request, resp)
	return resp, nil
}

// Sso 单点登录
func (l *Login) Sso(ctx context.Context, request *pb.SsoRequest) (*pb.SsoResponse, error) {
	resp, err := l.loginService.Sso(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc sso request:%+v, err:%s", request, err)
		return nil, err
	}
	return resp, nil
}
